using System;
using System.Collections.Generic;
using System.Windows.Forms;
using IFS_Gradebook_Generator.App_Code;

namespace IFS_Gradebook_Generator
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();

            CheckDatabaseStatus();
        }

        private bool CheckDatabaseStatus()
        {
            if (string.IsNullOrEmpty(Settings.Default.DatabasePath))
            {
                tslDatabaseStatus.Text = "No database selected";
                return false;
            }

            if (!System.IO.File.Exists(Settings.Default.DatabasePath))
            {
                tslDatabaseStatus.Text = "Database file does not exist: " + System.IO.Path.GetFileName(Settings.Default.DatabasePath);
                return false;
            }

            tslDatabaseStatus.Text = "Database file selected: " + System.IO.Path.GetFileName(Settings.Default.DatabasePath);
            return true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ddlGradeList.SelectedIndex = 0;

            if (CheckDatabaseStatus())
                GetAllStudents();
        }

        private void GetAllStudents()
        {
            List<Students> lstStudents = DataHelper.GetStudents(string.Empty);
            dgvClassList.DataSource = lstStudents;
            lblCount.Text = lstStudents.Count.ToString();
        }

        private void btnGetStudents_Click(object sender, EventArgs e)
        {
            if (ddlGradeList.Text != "" && ddlGradeList.Text != "ALL")
            {
                List<Students> lstStudents = DataHelper.GetStudents(ddlGradeList.Text);
                dgvClassList.DataSource = lstStudents;
                lblCount.Text = lstStudents.Count.ToString();
            }
            else
            {
                GetAllStudents();
            }               
        }

        private void btnCreateGradebook_Click(object sender, EventArgs e)
        {
            var lstStudents = dgvClassList.DataSource as List<Students>;

            if (lstStudents == null)
            {
                MessageBox.Show("No students available");
                return;
            }

            if (ddlGradeList.Text != "ALL")
            {
                int GradebooksCreated = 0;
                ExcelClass.CreateGradebook(ddlGradeList.Text, lstStudents, ref GradebooksCreated);
                MessageBox.Show("The " + ddlGradeList.Text + " gradebook was created.", "Alhamdulilah!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                int NumberOfGradebooksCreated = 0;

                foreach (string grade in ddlGradeList.Items)
                {
                    if (grade != "ALL")
                    {
                        ExcelClass.CreateGradebook(grade, lstStudents.FindAll(s => s.Grade.Equals(grade)), ref NumberOfGradebooksCreated);
                    }
                }
                
                MessageBox.Show(NumberOfGradebooksCreated + " gradebooks were created.", "Alhamdulilah!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }

        private void toolStripButton_Click(object sender, EventArgs e)
        {
            var fDialog = new OpenFileDialog
                              {
                                  Title = "Select Student Database",
                                  Filter = "Access Database Files|*.mdb|Access 2007 Database Files|*.accdb"
                              };

            if (fDialog.ShowDialog() == DialogResult.OK)
            {
                Settings.Default.DatabasePath = fDialog.FileName;
                Settings.Default.Save();
                tslDatabaseStatus.Text = "Database file selected: " + fDialog.SafeFileName;

                GetAllStudents();
            }
        }
    }
}